package org.brisling.service.sysman;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.brisling.config.common.CommonConst;
import org.brisling.domain.sysman.Roles;
import org.brisling.domain.sysman.Users;
import org.brisling.domain.sysman.inf.BizBeanDataModeler;
import org.brisling.domain.sysman.inf.ManageBeanDataModeler;
import org.brisling.domain.sysman.manage.UserRoleDomain;
import org.brisling.domain.sysman.relation.UserRoleRelationDomain;
import org.brisling.domain.sysman.user.UserConfigs;
import org.brisling.repository.sysman.inf.RolesRepo;
import org.brisling.service.common.BaseListByUplinkService;
import org.brisling.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/sysman/user")
/**
 * 用戶后台服務service
 * @Author : jackson wang
 * @Date   : 2015年6月8日 下午4:18:01
 * @version: 1.0
 */
public class UserService extends BaseListByUplinkService{
	
	
	
	@Autowired
	private MAndRService mrCtl;	
	
	
	@Autowired
	private RolesRepo rrepo;
	

	
	
	@RequestMapping("/saveManage")
	@ResponseBody
	/**
	 * 保存用户关联配置
	 * @param body 前端传递的json数据对象
	 * @return 操作状态码
	 */
	public String saveManage(			
			@RequestBody String body){				
		
		Users _user = JsonUtil.bodyNodeToClass(body, "user", Users.class);
		
		if(_user==null||_user.getId()==null)
			return "id must not be null";	
		
		
		List<UserRoleDomain> urrDomains = JsonUtil.bodyNodeToList(body, "roles", UserRoleDomain.class);
		mrCtl.persistMAndR(_user,urrDomains,CommonConst.MANAGE_TYPE,"userRole");
		
		
		return "200";		
	}
	
	
	@RequestMapping("/saveRelation")
	@ResponseBody
	/**
	 * 保存用户授权配置
	 * @param body 前端传递的json数据对象
	 * @return 操作状态码
	 */
	public String saveRelation(@RequestBody String body){
		
		Users _user = JsonUtil.bodyNodeToClass(body, "user", Users.class);
		
		if(_user==null||_user.getId()==null)
			return "id must not be null";			
		
		
		List<UserRoleRelationDomain> urrDomains = JsonUtil.bodyNodeToList(body, "roles", UserRoleRelationDomain.class);
		mrCtl.persistMAndR(_user,urrDomains,CommonConst.RELATION_TYPE,"userRole");
		
		
		return "200";		
	}
	
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@RequestMapping("/getRelation")
	@ResponseBody
	/**
	 * 获取用户授权
	 * @param body 前端传递的json数据对象
	 * @return UserConfigs 封装后的用户授权情况
	 */
	public UserConfigs getRelation(			
			@RequestBody String body){	
		
		Users _user = JsonUtil.bodyToObj(body, Users.class);
		if(_user==null||_user.getId()==null)
			return null;	
		
		
		List<? extends ManageBeanDataModeler> _roles = mrCtl.getListByBiztid(_user.getId(), CommonConst.MANAGE_TYPE,"userRole");
		
		List<? extends ManageBeanDataModeler> _existGroups =mrCtl.getListByBiztid(_user.getId(),CommonConst.RELATION_TYPE,"userGroup");
		List<? extends ManageBeanDataModeler> _existRoles = mrCtl.getListByBiztid(_user.getId(), CommonConst.RELATION_TYPE,"userRole");
		List<? extends ManageBeanDataModeler> _existPermissions = mrCtl.getListByBiztid(_user.getId(), CommonConst.RELATION_TYPE,"userPermission");
		
		List<? extends ManageBeanDataModeler> _unJoinedRoless = mrCtl.filerExistData(_roles, _existRoles) ;
		//UserGroupDomain
		//List<ManageBeanDataModeler> ugList = getCombinedDomain(UserGroupDomain.class,_user,Groups.class,group);
		
		//delManage(_uid);
		UserConfigs uc = new UserConfigs();
		
		uc.setRoles((List<UserRoleDomain>) _unJoinedRoless);
		
		uc.setExistGroups( _existGroups);
		uc.setExistRoles(_existRoles);
		uc.setExistPermissions(_existPermissions);
		
		return uc;		
	}
	
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@RequestMapping("/getManage")
	@ResponseBody
	/**
	 * 保存用户关联配置情况，根据body中传入的json类(<code>UserConfig</code>)进行后台数据同步	
	 * @param body
	 * @return  用户管理配置
	 */
	public UserConfigs getManage(			
			@RequestBody String body){		
		
		
		Users _user = JsonUtil.bodyToObj(body, Users.class);
		if(_user==null||_user.getId()==null)
			return null;	
		
		
		List<Roles> allRoles = rrepo.findAll();
		List<? extends ManageBeanDataModeler> _roles = mrCtl.getCombinedDomain(UserRoleDomain.class, _user, allRoles);
		
		
		List<? extends ManageBeanDataModeler> _existRoles = mrCtl.getListByBiztid(_user.getId(), CommonConst.MANAGE_TYPE,CommonConst.USER_ROLE_CODE);
		
		List<? extends ManageBeanDataModeler> _unJoinedRoless = mrCtl.filerExistData(_roles, _existRoles) ;
		//UserGroupDomain
		//List<ManageBeanDataModeler> ugList = getCombinedDomain(UserGroupDomain.class,_user,Groups.class,group);
		
		//delManage(_uid);
		UserConfigs uc = new UserConfigs();
		uc.setRoles((List<UserRoleDomain>) _unJoinedRoless);
		
		uc.setExistRoles(_existRoles);
		
		return uc;		
	}
	
	
	@SuppressWarnings({ "rawtypes", "unchecked", "unused" })
	/**
	 * 根据json数据对象拼装管理实体对象
	 * @param clazzMAndR	manytomany关联实体类
	 * @param bizt			关联实体主导对象
	 * @param clazze		关联实体关联类
	 * @param bizeContent	关联实体关联类JSON字符串
	 * @return ManageBeanDataModeler 
	 */
	private List<ManageBeanDataModeler> getCombinedDomain(
			Class<? extends ManageBeanDataModeler> clazzMAndR,
			BizBeanDataModeler bizt,Class clazze,
			String bizeContent){
		List<ManageBeanDataModeler> mmList = new ArrayList<ManageBeanDataModeler>();
		if(bizeContent!=null){
			List<BizBeanDataModeler> _bizeList =  JsonUtil.bodyToList(bizeContent, clazze);
			if(_bizeList!=null&&_bizeList.size()>0){
				 for(Iterator<BizBeanDataModeler> ite=_bizeList.iterator();ite.hasNext();){
					 ManageBeanDataModeler mrDomain = null;
					try {
						mrDomain = clazzMAndR.newInstance();
					} catch (InstantiationException | IllegalAccessException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					 mrDomain.setBizt(bizt);
					 mrDomain.setBize(ite.next());
					 mmList.add(mrDomain);
				 }
			 }
		}
		return mmList;
	}
	
	
	
	
}
